package com.zlq.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 元数据处理
 */
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("插入自动填充.....");
        log.info(metaObject.toString());
        this.strictInsertFill(metaObject, "createTime", () -> LocalDateTime.now(), LocalDateTime.class);
        this.strictInsertFill(metaObject, "updateTime", () -> LocalDateTime.now(), LocalDateTime.class);
        metaObject.setValue("createUser",BaseContext.getThreadLocalId());
        metaObject.setValue("updateUser",BaseContext.getThreadLocalId());

        //this.fillStrategy(metaObject, "createTime", LocalDateTime.now()); 也可以使用(3.3.0 该方法有bug)

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("跟新自动填充.....");
        log.info(metaObject.toString());
        //this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐)
        // 起始版本 3.3.3(推荐)
        this.strictUpdateFill(metaObject, "updateTime", () -> LocalDateTime.now(), LocalDateTime.class);
        metaObject.setValue("updateUser",BaseContext.getThreadLocalId());
        //this.fillStrategy(metaObject, "updateTime", LocalDateTime.now()); // 也可以使用(3.3.0 该方法有bug)

    }
}
